package com.kkb.mybatis.test.jdbc;

import java.sql.*;

public class JdbcDemo {

    public static void main(String[] args){
        jdbc();
    }

    public static void jdbc(){
    
        Connection conn = null;
        try{
            //获取驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kaike?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8","root","123456");
            
            String sql = "select * from user";

            PreparedStatement preparedStatement = conn.prepareStatement(sql);

            ResultSet resultSet = preparedStatement.executeQuery();
            
            while(resultSet.next()){
                System.out.println("name:"+resultSet.getString("name")+"    "+"gender:"+resultSet.getString("gender"));
            }   
        } catch(Exception e){
            e.printStackTrace();
        } finally{
                //关闭连接
                try {
                   conn.close();
                } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}